package monsmon.term;

import java.awt.Color;


/*
 * This is a drawing surface
 *
 */
public class Surface{
	
	private final Tchar __buff[][];
	private final int w, h;

	/*
	 * Create an empty surface of size (w, h)
	 */
	public Surface(int w, int h){
		this.w = w;
		this.h = h;
		__buff = new Tchar[w][h];
		for(int x = 0; x < w; x++){
			for(int y = 0; y < h; y++){
				__buff[x][y] = new Tchar();
			}
		}
	}
	
	/* Copy Constructor */
	public Surface(Surface from){
		this(from.w, from.h);
		for(int x = 0; x < w; x++){
			for(int y = 0; y < h; y++){
				Tchar.cp(from.__buff[x][y], this.__buff[x][y]);
			}
		}
	}


	public void draw(int x, int y, char c){
		draw(x, y, c, Color.WHITE, Color.BLACK, false, ' ', 
				Color.WHITE);
	}

	public void draw(int x, int y, char c, Color fg){
		draw(x, y, c, fg, Color.BLACK, false, ' ', Color.WHITE);
	}

	public void draw(int x, int y, char c, Color fg, Color bg){
		draw(x, y, c, fg, bg, false, ' ', Color.WHITE);
	}

	public void draw(int x, int y, char c, Color fg, Color bg, 
			boolean blink){
		draw(x, y, c, fg, bg, blink, ' ', Color.WHITE);
	}

	public void draw(int x, int y, char c, Color fg, boolean blink, 
			char altc){
		draw(x, y, c, fg, Color.BLACK, blink, altc, Color.WHITE);
	}

	public void draw(int x, int y, Tchar tc){
		draw(x, y, tc.c, tc.fg, tc.bg, tc.blink, tc.altc, tc.altfg);
	}

	public void draw(int x, int y, char c, Color fg, Color bg,
			boolean blink, char altc, Color altfg){
		if(x >= 0 && x < w){
			if(y >= 0 && y < h){
				Tchar tc = __buff[x][y];
				tc.c = c;
				tc.fg = fg;
				tc.bg = bg;
				tc.blink = blink;
				tc.altc = altc;
				tc.altfg = altfg;
			}
		}
	}

	public void clear(){
		for(int x = 0; x < w; x++){
			for(int y = 0; y < h; y++){
				Tchar c = __buff[x][y];
				c.c = ' ';
				c.fg = Color.WHITE;
				c.bg = Color.BLACK;
				c.blink = false;
				c.altc = ' ';
				c.altfg = Color.WHITE;
			}
		}
	}

	public void blit(Surface src, Rect src_area, int tx, int ty){
		if(src_area == null){
			blit(src, 0, 0, src.w, src.h, tx, ty);
		}else{
			blit(src, src_area.x, src_area.y, src_area.w, 
				src_area.h, tx, ty);
		}
	}

	public void blit(Surface src, int fx, int fy, int fw, int fh, 
			int tx, int ty){
		for(int x = 0; x + tx <	this.w && x + fx < src.w && 
				x + fx < fw; x++){
			for(int y = 0; y + ty < this.h && y + fx < src.h && 
					y + fy < fh; y++){
				Tchar.cp(src.__buff[x + fx][y + fy], 
						this.__buff[x + tx][y + fy]);
			}
		}
	}
	
	public void fill_rect(Rect r, char c, Color fg, Color bg){
		if(r != null){
			fill_rect(r.x, r.y, r.w, r.h, c, fg, bg);
		}else{
			fill_rect(0, 0, w, h, c, fg, bg);
		}
	}

	public void fill_rect(int x, int y, int w, int h, char c, Color fg, 
			Color bg){
		for(int x_ = x; x_ < w && x_ < this.w; x_++){
			for(int y_ = y; y_ < h && y_ < this.h; y_++){
				Tchar tc = __buff[x_][y_];
				tc.c = c;
				tc.fg = fg;
				tc.bg = bg;
			}
		}
	}
	

	public Tchar get(int x, int y){
		return __buff[x][y];
	}

	public int get_w(){
		return w;
	}

	public int get_h(){
		return h;
	}
	
	public void __dump_buffer(){
		for(int y = 0; y < h; y++){
			for(int x = 0; x < w; x++){
				System.out.print(__buff[x][y].c);
			}
			System.out.println();
		}
	}

	protected void __fast_blit(Surface to){
		for(int x = 0; x < w; x++){
			for(int y = 0; y < h; y++){
				Tchar.cp(__buff[x][y], to.__buff[x][y]);
			}
		}
	}

}
